define(["moment"], function (moment) {
    return ['$rootScope', "$scope", "$filter", "$stateParams", "config", "MMWService",
        function ($rootScope, $scope, $filter, $stateParams, config, $MMWService) {
            $scope.workItemList = [];
            $scope.PageConfig= {
                page: 0,
                page_count: 25
            },
            // $scope.filterItem = [],
            $scope.init = function () {
                $scope.C00_require_condition = true;
                var datasource_format_info = {};
                datasource_format_info.iselectd_filed = "0";
                datasource_format_info.column_info = [$filter('translate')('WIP121.Dialog.StateDialog.Code'), $filter('translate')('WIP121.Dialog.StateDialog.Name')];
                pageNumberInfoGet('equipmentstate', function (itemList) {
                    console.log(itemList);
                    datasource_format_info.data_info = itemList.map(function (item) {
                        return [item.state_no, item.state_name];
                    });
                });
                $scope.codeList = [
                    { name: $filter('translate')('WIP121.query.EquipmentNo'), column_name: 'EESL.EquipmentNo', data_type: "" },
                    { name: $filter('translate')('WIP121.query.State'), column_name: 'EESL.Equipmentstate', data_type: "S", datasource_format_info: datasource_format_info },
                    { name: $filter('translate')('WIP121.query.DateRange'), column_name: 'EESL.StartTime', data_type: 'SE' }
                ];
            }

            $scope.back = function () {
                //返回首頁，傳入true表示使用退出
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);//動畫
            }

            $scope.confirm = function () {
                //確認按鈕
                var details = [];
                $scope.workItemList.forEach(function (obj) { //如果有修改 才裝進datails
                    if (obj.IsChange) {
                        details.push(obj);
                    }
                });
                if (details.length > 0) { //如果有資料 才裝進content
                    var content = {
                        details: details
                    };

                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_WIP.eqp_status_adjust_process',
                        content: content,
                        success: function (data) {
                            $scope.$root.showToast($filter('translate')('common.msg.success'));
                            var condition = $scope.$root.genConditioninfo();
                            $scope.search(condition);
                        }
                    });
                } else { //沒資料 show'請請選擇一筆資料'
                    $scope.$root.showAlert($filter('translate')('common.msg.plz_select_one'));
                };
            }

            //sMES 2.1.0 以後新增
            $scope.final = function (callback) {
                //當離開此程式的時候一定會執行此函式
                callback();//結束程式
            }

            $scope.RowClass = function (item) {
                return item.IsChange ? "red" : "";
            }

            $scope.search = function (condition) {
                var content = {
                    query_condition: condition
                };
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.eqp_status_adjust_info_get',
                    content: content,
                    success: function (data) {
                        if (data.details && data.details.length > 0) {
                            $scope.workItemList = data.details;
                            $scope.isSelectAll = false;
                        } else {
                            $scope.workItemList = [];
                            $scope.isSelectAll = false;
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        };
                        //把查到的資料再處理 加上index識別每筆資料 加上lastReasonNo判斷item.ReasonNumber是否被修改
                        // keys = Object.keys($scope.workItemList);
                        for (var i = 0; i < $scope.workItemList.length; i++) { 
                            $scope.workItemList[i].index = i;                           
                            $scope.workItemList[i].lastReasonNo = $scope.workItemList[i].ReasonNumber;      
                            $scope.workItemList[i].StateName = $filter('translate')(`WIP121.state.state_no_${$scope.workItemList[i].State}`)                    
                        };
                    }
                });
            };

            $scope.loadStateNo = function (item) {
                $rootScope.showQueryList({
                    header: [
                        { label: $filter('translate')('WIP121.Dialog.StateDialog.Code'), code: 'state_no' },
                        { label: $filter('translate')('WIP121.Dialog.StateDialog.Name'), code: 'state_name' }
                    ],
                    query: function (conditions, feedback) {
                        pageNumberInfoGet('equipmentstate', feedback);
                    },
                    confirm: function (selectItem, query_dialog) {
                        if (item.LastState && item.LastState != selectItem.state_no) {
                            item.IsChange = true;
                        }
                        else if (item.state != selectItem.state_no) {
                            item.IsChange = true;
                        }
                        item.State = selectItem.state_no;
                        item.StateName = selectItem.state_name;
                        item.ReasonNumber = "";
                        item.ReasonName = "";
                        query_dialog.hide();
                    }
                });
            };

            $scope.loadReasonNo = function (item) {
                var reasonSubType = GetReasonSubType(item.State);
                $rootScope.showQueryList({
                    header: [
                        { label: $filter('translate')('WIP121.Dialog.ReasonDialog.Code'), code: 'reasonno' },
                        { label: $filter('translate')('WIP121.Dialog.ReasonDialog.Name'), code: 'reasonname' }
                    ],
                    query: function (conditions, feedback) {
                        var query_condition_info = [];
                        query_condition_info.push({ column_name: 'ReasonType', query_mode: '0', value: '6', merge_condition_model: 'AND' });
                        query_condition_info.push({ column_name: 'ReasonSubType', query_mode: '0', value: reasonSubType, merge_condition_model: 'AND' });
                        pageNumberInfoGet('reason_basis', feedback, query_condition_info);
                    },
                    confirm: function (selectItem, query_dialog) {
                        if (item.ReasonNumber != selectItem.ReasonNumber) {
                            item.IsChange = true;
                        }
                        item.ReasonNumber = selectItem.reasonno;
                        item.ReasonName = selectItem.reasonname;
                        query_dialog.hide();
                    }
                });
            };
            //由item.state來決定該input是否disable
            $scope.DisabledReason = function (state) {
                return !GetReasonSubType(state);
            }

            function GetReasonSubType(state) {
                var result = '';
                switch (state.toString()) {
                    case "2":
                        result = "DOWN";
                        break;
                    case "3":
                        result = "REPAIR";
                        break;
                    case "6":
                        result = "SETUP";
                        break;
                    case "4":
                        result = "MAINTAIN";
                        break;
                    case "5":
                        result = "SUSPEND";
                        break;
                }
                return result;
            }

            function pageNumberInfoGet(page_mode, callback, query_condition_info) {
                if (!query_condition_info) {
                    query_condition_info = [];
                }
                $MMWService.pageNumberInfoGet({
                    page_mode: page_mode,
                    query_condition_info: query_condition_info
                }, function (itemList) {
                    // 20230801 modify by wenwei for M#141700 pageNumberInfoGet 沒有翻譯功能 改在前端直接翻譯
                    itemList = itemList.map(item => {
                        return {state_no:item.state_no, state_name:$filter('translate')(`WIP121.state.state_no_${item.state_no}`)}
                    })
                    if (callback)
                        callback(itemList);
                });
            }

            $scope.getPages = function(list, PageConfig, filterItem){
                if (list) {
                    // var temp_list = list.filter(function (item, index, array) {
                    //     var isOK = true,
                    //         keys = Object.keys(filterItem);
                    //     for (var i = 0; i < keys.length; i++) {
                    //         var key = keys[i];
                    //         if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                    //             if (item[key].indexOf(filterItem[key]) == -1) {
                    //                 isOK = false;
                    //                 break;
                    //             }
                    //         }
                    //     }
                    //     return isOK;
                    // });
                    if (PageConfig) {
                        PageConfig.total_page = Math.ceil(list.length / PageConfig.page_count) || 1; //最大頁數 (陣列長度/每頁比數)
                        if (PageConfig.page >= PageConfig.total_page) {
                            PageConfig.page = 0;
                        }
                        var index = PageConfig.page * PageConfig.page_count;
                        return list.slice(index, index + PageConfig.page_count); //回傳該頁數的內容
                    } else {
                        return list;
                    }
                } else {
                    return list;
                }
            }

        }
    ]
});